<?php

namespace console\controllers;

use Yii;
use yii\console\Controller;
use common\models\statistics\StatisticsDoctor;
use services\clinic\ClinicDoctorService;
use services\clinic\UserTreatmentService;

/**
 * 医生
 *
 * Class PatientController
 * @package console\controllers
 */
class DoctorController extends Controller
{
    /**
     * 统计每日报表【每日凌晨1点】
     *
     * 0 1 * * * php yii doctor/create-day >/dev/null 2>&1
     */
    public function actionCreateDay()
    {
        try {
            $startTime = time();

            $date = date('Y-m-d', $startTime - 86400);

            $service = new ClinicDoctorService();
            $service->createReport([
                'type' => StatisticsDoctor::TYPE_DAY,
                'start_time' => $date,
                'end_time' => $date,
            ]);

            $endTime = time();
            $diffTime = $endTime - $startTime;

            echo "success".PHP_EOL;
            echo "开始时间: {$startTime}".PHP_EOL;
            echo "结束时间: {$endTime}".PHP_EOL;
            echo "本次耗时: {$diffTime}".PHP_EOL;
        } catch (\Exception $e) {
            echo 'error: '.$e->getMessage().PHP_EOL;
        }
    }

    /**
     * 统计上旬报表【11号凌晨1点】
     *
     * 0 1 11 * * php yii doctor/create-early-month
     */
    public function actionCreateEarlyMonth()
    {
        try {
            $startTime = time();

            $date = date('Y-m-d', $startTime - 86400);
            list($year, $month, $day) = explode('-', $date);

            $service = new ClinicDoctorService();
            $service->createReport([
                'type' => StatisticsDoctor::TYPE_EARLY_MONTH,
                'start_time' => "{$year}-{$month}-01",
                'end_time' => $date,
            ]);

            $endTime = time();
            $diffTime = $endTime - $startTime;

            echo "success".PHP_EOL;
            echo "开始时间: {$startTime}".PHP_EOL;
            echo "结束时间: {$endTime}".PHP_EOL;
            echo "本次耗时: {$diffTime}".PHP_EOL;
        } catch (\Exception $e) {
            echo 'error: '.$e->getMessage().PHP_EOL;
        }
    }

    /**
     * 统计中旬报表【21号凌晨1点】
     *
     * 0 1 21 * * php yii doctor/create-middle-month
     */
    public function actionCreateMiddleMonth()
    {
        try {
            $startTime = time();

            $date = date('Y-m-d', $startTime - 86400);
            list($year, $month, $day) = explode('-', $date);

            $service = new ClinicDoctorService();
            $service->createReport([
                'type' => StatisticsDoctor::TYPE_MIDDLE_MONTH,
                'start_time' => "{$year}-{$month}-01",
                'end_time' => $date,
            ]);

            $endTime = time();
            $diffTime = $endTime - $startTime;

            echo "success".PHP_EOL;
            echo "开始时间: {$startTime}".PHP_EOL;
            echo "结束时间: {$endTime}".PHP_EOL;
            echo "本次耗时: {$diffTime}".PHP_EOL;
        } catch (\Exception $e) {
            echo 'error: '.$e->getMessage().PHP_EOL;
        }
    }

    /**
     * 统计全月报表【1号凌晨1点】
     *
     * 0 1 1 * * php yii doctor/create-all-month
     */
    public function actionCreateAllMonth()
    {
        try {
            $startTime = time();

            $date = date('Y-m-d', $startTime - 86400);
            list($year, $month, $day) = explode('-', $date);

            $service = new ClinicDoctorService();
            $service->createReport([
                'type' => StatisticsDoctor::TYPE_ALL_MONTH,
                'start_time' => "{$year}-{$month}-01",
                'end_time' => $date,
            ]);

            $endTime = time();
            $diffTime = $endTime - $startTime;

            echo "success".PHP_EOL;
            echo "开始时间: {$startTime}".PHP_EOL;
            echo "结束时间: {$endTime}".PHP_EOL;
            echo "本次耗时: {$diffTime}".PHP_EOL;
        } catch (\Exception $e) {
            echo 'error: '.$e->getMessage().PHP_EOL;
        }
    }

    /**
     * 修复报表数据
     *
     * php yii doctor/repair-data 2014-03-09
     */
    public function actionRepairData($startDate = '')
    {
        try {
            $beginTime = time();

            // 首次来院时间
            $startDate = $startDate ?: '2014-03-09';
            $startTime = strtotime($startDate);

            $endDate = date('Y-m-d');
            $endTime = strtotime($endDate);

            $service = new ClinicDoctorService();
            while (true) {
                $date = date('Y-m-d', $startTime);
                list($year, $month, $day) = explode('-', $date);
                // 月份天数
                $maxDay = date('t', $startTime);

                echo "当前日期: {$date}";
                echo PHP_EOL;

                // 日报表
                $service->createReport([
                    'type' => StatisticsDoctor::TYPE_DAY,
                    'start_time' => $date,
                    'end_time' => $date,
                ]);

                // 上旬报表
                if ($day == 10) {
                    $service->createReport([
                        'type' => StatisticsDoctor::TYPE_EARLY_MONTH,
                        'start_time' => "{$year}-{$month}-01",
                        'end_time' => $date,
                    ]);
                }

                // 中旬报表
                if ($day == 20) {
                    $service->createReport([
                        'type' => StatisticsDoctor::TYPE_MIDDLE_MONTH,
                        'start_time' => "{$year}-{$month}-01",
                        'end_time' => $date,
                    ]);
                }

                // 全月报表
                if ($day == $maxDay) {
                    $service->createReport([
                        'type' => StatisticsDoctor::TYPE_ALL_MONTH,
                        'start_time' => "{$year}-{$month}-01",
                        'end_time' => $date,
                    ]);
                }

                $startTime += 86400;
                if ($startTime >= $endTime) {
                    break;
                }
            }

            $endingTime = time();
            $diffTime = $endingTime - $beginTime;

            echo "success" . PHP_EOL;
            echo "开始时间: {$beginTime}" . PHP_EOL;
            echo "结束时间: {$endingTime}" . PHP_EOL;
            echo "本次耗时: {$diffTime}" . PHP_EOL;
        } catch (\Exception $e) {
            echo 'error: ' . $e->getMessage() . PHP_EOL;
        }
    }

    /**
     * 修复患者治疗信息表【clinic_id】
     *
     * php yii doctor/repair-clinic-data
     */
    public function actionRepairClinicData()
    {
        try {
            $startTime = time();

            $service = new UserTreatmentService();
            $service->repairClinicId();

            $endTime = time();
            $diffTime = $endTime - $startTime;

            echo "success".PHP_EOL;
            echo "开始时间: {$startTime}".PHP_EOL;
            echo "结束时间: {$endTime}".PHP_EOL;
            echo "本次耗时: {$diffTime}".PHP_EOL;
        } catch (\Exception $e) {
            echo 'error: '.$e->getMessage().PHP_EOL;
        }
    }
}
